package com.xy.generate;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import com.xy.generate.meta.Meta;
import com.xy.generate.meta.MetaManager;
import com.xy.generate.generator.DynamicFileGenerator;

import java.io.File;

public class Main {
    public static void main(String[] args) {
        //test10();
        //testConfigCommandGenerate();
        //testCommandExecutorGenerate();
        //testListCommandGenerate();
        //testGenerateCommandGenerate();
        //testDynamicFileGeneratorGenerate();
        //testStaticFileGeneratorGenerate();
        //testMainGeneratorGenerate();
        //testPomXmlGenerate();
        testMainGenerate();
    }

    public static void test10() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String absolutePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        if (!FileUtil.exist(outputJavaBasePackagePath)) {
            FileUtil.mkdir(outputJavaBasePackagePath);
        }
        String inputFilePath = absolutePath + File.separator + "templates/java/model/DataModel.java.ftl";
        String outputFilePath = outputJavaBasePackagePath + "/model/DataModel.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testConfigCommandGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/children/ConfigCommand.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "cli/command/children/ConfigCommand.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testGenerateCommandGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/children/GenerateCommand.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "cli/command/children/GenerateCommand.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testListCommandGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/children/ListCommand.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "cli/command/children/ListCommand.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testCommandExecutorGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/cli/command/CommandExecutor.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "cli/command/CommandExecutor.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testDynamicFileGeneratorGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/generator/DynamicFileGenerator.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "generator/DynamicFileGenerator.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testMainGeneratorGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/generator/MainGenerator.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "generator/MainGenerator.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testStaticFileGeneratorGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/generator/StaticFileGenerator.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "generator/StaticFileGenerator.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testPomXmlGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/pom.xml.ftl";
        //输出路径
        String outputFilePath = outputPath + File.separator + "pom.xml";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }

    public static void testMainGenerate() {
        Meta instance = MetaManager.getInstance();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + instance.getName();
        //读取resource
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();
        //java包基础路径
        String outputBasePackage = instance.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/", StrUtil.split(outputBasePackage, "."));
        String outputJavaBasePackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;
        //读取路径
        String inputFilePath = inputResourcePath + File.separator + "templates/java/Main.java.ftl";
        //输出路径
        String outputFilePath = outputJavaBasePackagePath + File.separator + "Main.java";
        DynamicFileGenerator.doGenerate(inputFilePath, outputFilePath, instance);
    }
}